package com.tadpole.music.view.activity.web;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.tadpole.music.R;
import com.tadpole.music.bean.PushBean;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.widget.LollipopFixedWebView;

/**
 * Created By 孙岩
 * on 2020/12/5
 */
public class H5Activity extends BaseActivity {
    private View viewBack;
    private LollipopFixedWebView webView;
    private WebSettings webSettings;
    //标题
    private TextView tvTitle;

    private ImageView ivImage;

    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_h5);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        initViews();
        initListeners();

        webSettings = webView.getSettings();
        webSettings.setBuiltInZoomControls(false);
        webSettings.setSupportZoom(false);
        webSettings.setDisplayZoomControls(false);
        webSettings.setJavaScriptEnabled(true);//允许使用js
        webView.addJavascriptInterface(this, "Android");
        webView.setWebChromeClient(
                new WebChromeClient() {
                    public void onProgressChanged(WebView view, int progress) {// 载入进度改变而触发
                        if (progress == 100) {
                            ivImage.setVisibility(View.GONE);
                        } else {
                            ivImage.setVisibility(View.VISIBLE);
                        }

                        super.onProgressChanged(view, progress);
                    }
                });


    }

    //初始化控件
    @SuppressLint("JavascriptInterface")
    private void initViews(){
        viewBack = findViewById(R.id.viewBack);
        webView = findViewById(R.id.webView);
        tvTitle = findViewById(R.id.tvTitle);
        ivImage = findViewById(R.id.ivImage);

        PushBean pushBean = (PushBean) getIntent().getSerializableExtra("params");
        tvTitle.setText(pushBean.getTitle());
        String url = pushBean.getUrl();
        webView.loadUrl(url);
    }

    //初始化监听器
    private void initListeners(){
        viewBack.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                H5Activity.this.finish();
            }
        });
    }
}

